package genedit.gui.propertyDialogs;


import genedit.gui.GMainFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

@SuppressWarnings("serial")
public class DialogToolbar extends JToolBar {

	AbstractDialog parent;
	

	
	JButton btnPrvi;
	JButton btnSledeci;
	JButton btnPrethodni;
	JButton btnPoslednji;
	
	JButton btnDodavanje;
	JButton btnIzmena;
	JButton btnBrisanje;
	
	
	public DialogToolbar(AbstractDialog parent) {
		super();
		this.parent = parent;
		
		
		btnPrvi = new JButton();
		btnPrvi.setIcon(new ImageIcon("resources/icons/home_16.gif"));
		btnPrvi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog){
				if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
						((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(0, 0, false, false);
				}
				else {
					if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
						((ClassModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(0, 0, false, false);
				}
				}
				else if(DialogToolbar.this.parent instanceof InterfaceModelDialog){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
						if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
							((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(0, 0, false, false);
					}
					else {
						if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
							((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(0, 0, false, false);
					}
					}
			}
		});
		btnPrvi.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnPrvi"));
		btnPrvi.setMnemonic(KeyEvent.VK_P);
		add(btnPrvi);
		
		
		btnSledeci = new JButton();
		btnSledeci.setIcon(new ImageIcon("resources/icons/down_16.gif"));
		btnSledeci.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog) {
					if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else if(i < ((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()-1)
					((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(i+1, 0, false, false);
			}
					} else {
						if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
						{
						int i = ((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
						if(i == -1)
							JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
									 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
						else if(i < ((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()-1)
							((ClassModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(i+1, 0, false, false);
					}
					}
			}
				else if(DialogToolbar.this.parent instanceof InterfaceModelDialog) {
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else if(i < ((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()-1)
					((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(i+1, 0, false, false);
			}
					} else {
						if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
						{
						int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
						if(i == -1)
							JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
									 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
						else if(i < ((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()-1)
							((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(i+1, 0, false, false);
					}
					}
			}
			}
		});
		btnSledeci.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnSledeci"));
		btnSledeci.setMnemonic(KeyEvent.VK_D);
		add(btnSledeci);	
		
		
		btnPrethodni = new JButton();		
		btnPrethodni.setIcon(new ImageIcon("resources/icons/up_16.gif"));
		btnPrethodni.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog){
				if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
				if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else if(i > 0)
					((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(i-1, 0, false, false);				
				}
				} else {
					if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
					{
					int i = ((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
					if(i == -1)
						JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
								 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
					else if(i > 0)
						((ClassModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(i-1, 0, false, false);				
					}
				}
			}
				else if(DialogToolbar.this.parent instanceof InterfaceModelDialog){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
						if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
						{
						int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
						if(i == -1)
							JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
									 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
						else if(i > 0)
							((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(i-1, 0, false, false);				
						}
						} else {
							if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
							{
							int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
							if(i == -1)
								JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
										 GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
							else if(i > 0)
								((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(i-1, 0, false, false);				
							}
						}
					}
			}
		});

		btnPrethodni.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnPrethodni"));
		btnPrethodni.setMnemonic(KeyEvent.VK_T);
		add(btnPrethodni);
		
		
		btnPoslednji = new JButton();
		btnPoslednji.setIcon(new ImageIcon("resources/icons/end_16.gif"));
	btnPoslednji.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog){
					if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
						((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(
								((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()-1, 0, false, false);				
					}
					else {
						if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
							((ClassModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(
									((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()-1, 0, false, false);		
					}
				}
				else if(DialogToolbar.this.parent instanceof InterfaceModelDialog){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
						((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.changeSelection(
								((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()-1, 0, false, false);				
					}
					else {
						if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
							((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.changeSelection(
									((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()-1, 0, false, false);		
					}
				}
			}
		});
		btnPoslednji.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnPoslednji"));
		btnPoslednji.setMnemonic(KeyEvent.VK_C);
		add(btnPoslednji);	
		
		addSeparator();
		
		btnDodavanje = new JButton();
		btnDodavanje.setIcon(new ImageIcon("resources/icons/add_16.gif"));
		 
		
		btnDodavanje.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DialogToolbar.this.parent.dodavanje();
			}
		});
		btnDodavanje.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnDodavanje"));
		btnDodavanje.setMnemonic(KeyEvent.VK_N);
		add(btnDodavanje);
		
		
		 
		btnIzmena = new JButton();
		btnIzmena.setIcon(new ImageIcon("resources/icons/update_16.gif"));
		btnIzmena.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog){
					if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.izmena();	
				}
			} else {
				if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.izmena();	
				}
			}
			}
				else if(DialogToolbar.this.parent instanceof InterfaceModelDialog){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.izmena();	
				}
			} else {
				if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
				{
				int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.izmena();	
				}
			}
			}
			}
		});
		btnIzmena.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnIzmena"));
		btnIzmena.setMnemonic(KeyEvent.VK_I);
		add(btnIzmena);
		
		btnBrisanje = new JButton();
		btnBrisanje.setIcon(new ImageIcon("resources/icons/remove_16.gif"));
		btnBrisanje.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(DialogToolbar.this.parent instanceof ClassModelDialog){
					if(((ClassModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.brisanje();
				}
			} else {
				if(((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
				{
				int i = ((ClassModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.brisanje();
				}
			}
			}
				else 	if(DialogToolbar.this.parent instanceof InterfaceModelDialog){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).jtabbed.getSelectedIndex()==0){
					if(((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getRowCount()!=0)
				{
				int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableAttributes.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.brisanje();
				}
			} else {
				if(((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getRowCount()!=0)
				{
				int i = ((InterfaceModelDialog)DialogToolbar.this.parent).tableMethods.getSelectedRow() ;
				if(i == -1)
					JOptionPane.showMessageDialog(null,GMainFrame.getInstance().getResourceBundle().getString("nijeSelektovan"),
							GMainFrame.getInstance().getResourceBundle().getString("selekcija"), JOptionPane.ERROR_MESSAGE);
				else
				DialogToolbar.this.parent.brisanje();
				}
			}
			}
			}
		});
		btnBrisanje.setToolTipText(GMainFrame.getInstance().getResourceBundle().getString("btnBrisanje"));
		btnBrisanje.setMnemonic(KeyEvent.VK_B);
		add(btnBrisanje);	

		
	}
	
}
